package com.sjx.onelinecord

import android.content.pm.PackageManager
import android.os.Bundle
import android.provider.ContactsContract
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.activity_read_address_book_aty.*

class ReadAddressBookAty : AppCompatActivity() {
    private val contactsList = ArrayList<AddressBook>()
    private lateinit var addressBook: AddressBook
    private var adapter: AddressBookAdapter? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_read_address_book_aty)
        adapter = AddressBookAdapter(contactsList)
        var layoutManager = LinearLayoutManager(this)
        contactsView.layoutManager = layoutManager
        contactsView.adapter = adapter
        if (ContextCompat.checkSelfPermission(this,
                        android.Manifest.permission.READ_CONTACTS)
                != PackageManager.PERMISSION_GRANTED
        ) {
            ActivityCompat.requestPermissions(this,
                    arrayOf(android.Manifest.permission.READ_CONTACTS), 1
            )
        } else {
            readContacts()
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int,
                                            permissions: Array<out String>,
                                            grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        when (requestCode) {
            1 -> {
                if (grantResults.isNotEmpty()
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    readContacts()
                } else {
                    Toast.makeText(this, "You denied the permission",
                            Toast.LENGTH_LONG
                    ).show()
                }
            }
        }
    }

    private fun readContacts() {
        contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                null, null,
                null, null)?.apply {
            while (moveToNext()) {
                val displayName = getString(getColumnIndex(ContactsContract.CommonDataKinds
                        .Phone.DISPLAY_NAME))
                val number = getString(getColumnIndex(ContactsContract.CommonDataKinds
                        .Phone.NUMBER))
                addressBook = AddressBook(displayName, number)
                Log.i("ReadAddressBookAty", displayName + number)
                addressBook?.displayName = displayName
                addressBook?.number = number
                Log.i("ReadAddressBookAty",
                        addressBook?.displayName + addressBook?.number)
                if (addressBook != null) {
                    contactsList.add(addressBook)
                }
            }
            adapter?.notifyDataSetChanged()
            close()
        }
    }
}